import { UserService } from '../../services'
import { USERNAME_TEST, PASSWORD_TEST } from '../../constance/Reg'
import localCache from '../../utils/localCache'
export default {
  data() {
    return {
      username: '米超强',
      password: 'mima123456',
      usernamePattern: USERNAME_TEST,
      passwordPattern: PASSWORD_TEST,
      uploader: []
    }
  },
  methods: {
    async onSubmit(values) {
      console.log(values)
      const newFile = values.uploader[0].file
      const formData = new FormData()
      formData.append('avatar', newFile)
      const res1 = await UserService.registry({
        username: values.name,
        password: values.password
      })
      if (res1.data.status === 200 && res1.data.msg === '注册用户成功') {
        const res = await UserService.login(values)
        if (res.data.status === 200 && res.data.msg === '登录成功') {
          localCache.setLocalCache('miBlogToken', res.data.token)
          this.$store.commit('isLogin')
          this.$router.push('/')
          this.$notify({ type: 'success', message: res.data.msg })
          await UserService.upLoadUserAvatar(formData)
        } else {
          this.$notify({ type: 'danger', message: res.data.msg })
        }
      } else {
        this.$notify({ type: 'danger', message: res1.data.msg })
      }
    },
    gitFile() {
      console.log(this.uploader)
    },
    onClickLeft() {
      this.$router.push('/login')
    },
    goLogin() {
      this.$router.push('/login')
    }
  }
}
